//
//  LCMyTaskGameViewController.m
//  lechuang
//
//  Created by Mr_hi on 2023/4/12.
//  Copyright © 2023 Mooyi. All rights reserved.
//

#import "LCMyTaskGameViewController.h"
#import "LCTaskModel.h"
#import "LCTaskService.h"
#import "LCMyTaskTableViewCell.h"

@interface LCMyTaskGameViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (assign, nonatomic)NSInteger page;

@property (strong, nonatomic)NSMutableArray * dataArr;

@end

@implementation LCMyTaskGameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.page = 1;
    [self setTableViewRefresh];
    [self getMyTaskGameDataWithPage:1];
}

- (IBAction)backClick:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)setTableViewRefresh {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.page = 1;
        [self getMyTaskGameDataWithPage:self.page];
    }];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        self.page++;
        [self getMyTaskGameDataWithPage:self.page];
    }];
    self.tableView.mj_footer.automaticallyChangeAlpha = YES;
    
    self.tableView.ly_emptyView = [LCEmptyView emptyViewWithImageStr:nil titleStr:nil detailStr:@"暂无数据"];
}

-(void)getMyTaskGameDataWithPage:(NSInteger)page {
    [LCTaskService getMyTaskGameListWithAppId:self.dyUnionModel.config.iosappid Key:self.dyUnionModel.config.iosappsecret PageNum:page Block:^(BOOL isSuccess, NSArray<LCGameTaskModel *> *list) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (isSuccess) {
            if (page == 1) {
                [self.dataArr removeAllObjects];
            }
            [self.dataArr addObjectsFromArray:list];
            if (list.count < 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            [self.tableView reloadData];
        } else {
            if (self.page != 1) {
                self.page--;
            }
        }
    }];
}

-(NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCMyTaskGameTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"LCMyTaskGameTableViewCell"];
    cell.task = self.dataArr[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%ld",indexPath.row);
}

@end
